<template>
  <div class="update-name">
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onConfirm"
    />
    <div class="field-wrap">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import {updateUserprofile} from '@/api/user'
export default {
  data() {
    return {
      message: this.value,
    };
  },
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true, // 禁止背景点击
        duration: 0, // 持续展示
      });
      try {
        const message = this.message;
        if (!message.length) {
          this.$toast("昵称不能为空");
        }
        let res = await updateUserprofile({
          name: message,
        });
        this.$emit("input", message);
        this.$emit("close");
        this.$toast.success("修改成功");
      } catch (error) {
        this.$toast("修改失败,请重试");
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>